package com.example.demo.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author by itheima
 * @Date 2022/4/30
 * @Description 定义文件上传的处理器类
 */
@RestController
public class UploadController {

    @PostMapping("/upload")
    public String upload(String userName, MultipartFile imgFile) throws IOException {
        //获取原始文件的名称
        String originalFilename = imgFile.getOriginalFilename();
        //获取输入框name属性值
        String name = imgFile.getName();
        //获取文件后缀
        int index = originalFilename.lastIndexOf(".");
        String sufixType = originalFilename.substring(index);
        //保存到本地
        String newName="C:\\Users\\46035\\Desktop\\imgs\\"+ UUID.randomUUID().toString()+sufixType;
        //转存到指定磁盘路径
        imgFile.transferTo(new File(newName));
        return "success";
    }

}
